import pandas as pd
import numpy as np
import pyecharts.options as opts
from pyecharts.charts import Line
# from pyecharts.commons.utils import JsCode

class LineChart_flow_1year():
    
    def __init__(self, dataset, place=0, year='2015'):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        self.year=year

        #主程序入口
        self.main()
    
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘图函数
        self.paint()
        
        
    def getData(self):
            
        df=self.paint_data.loc[:,['date','flow']] #提取日期和客流数据
        self.paint_data=df.loc[df['date'].str.contains(self.year)] #筛选指定年份的数据


    def paint(self):

        x_data = np.array(self.paint_data.loc[:,'date']).tolist()
        y_data = np.array(self.paint_data.loc[:,'flow']).tolist()
        
        
        # background_color_js = (
        #     "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
        #     "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
        # )
        # area_color_js = (
        #     "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
        #     "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
        # )
        
        if self.place==0:
            title='九寨沟'+self.year+'年单日客流量'
        else:
            title='四姑娘山'+self.year+'年单日客流量'     
        
        fileName='1.3 (L1400)'+title+"折线图.html"
        
        c = (
            Line(init_opts=opts.InitOpts(width="1400px", height="600px"))
            .add_xaxis(xaxis_data=x_data)
            .add_yaxis(
                "客流",
                y_data,
                symbol="triangle",
                symbol_size=8,
                linestyle_opts=opts.LineStyleOpts(color="#00CED1", width=1.5),
                itemstyle_opts=opts.ItemStyleOpts(
                    border_width=1, border_color="#000000", color="#000000"
                ),
            )
            .set_global_opts(title_opts=opts.TitleOpts(title=title))
            .render('resultFile/'+fileName)
        )
        
        # c = (
        #     Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js),width="1400px", height="600px"))
        #     # Line(init_opts=opts.InitOpts(width="1400px", height="600px"))
        #     .add_xaxis(xaxis_data=x_data)
        #     .add_yaxis(
        #         series_name="单日客流量",
        #         y_axis=y_data,
        #         is_smooth=True,
        #         is_symbol_show=True,
        #         symbol="circle",
        #         symbol_size=6,
        #         linestyle_opts=opts.LineStyleOpts(color="#fff"),
        #         label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
        #         itemstyle_opts=opts.ItemStyleOpts(
        #             color="red", border_color="#fff", border_width=3
        #         ),
        #         tooltip_opts=opts.TooltipOpts(is_show=False),
        #         areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
        #     )
        #     .set_global_opts(
        #         title_opts=opts.TitleOpts(
        #             title=title,
        #             pos_bottom="5%",
        #             pos_left="center",
        #             title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
        #         ),
        #         xaxis_opts=opts.AxisOpts(
        #             type_="category",
        #             boundary_gap=False,
        #             axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
        #             axisline_opts=opts.AxisLineOpts(is_show=False),
        #             axistick_opts=opts.AxisTickOpts(
        #                 is_show=True,
        #                 length=25,
        #                 linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
        #             ),
        #             splitline_opts=opts.SplitLineOpts(
        #                 is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
        #             ),
        #         ),
        #         yaxis_opts=opts.AxisOpts(
        #             type_="value",
        #             position="right",
        #             axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
        #             axisline_opts=opts.AxisLineOpts(
        #                 linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
        #             ),
        #             axistick_opts=opts.AxisTickOpts(
        #                 is_show=True,
        #                 length=15,
        #                 linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
        #             ),
        #             splitline_opts=opts.SplitLineOpts(
        #                 is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
        #             ),
        #         ),
        #         legend_opts=opts.LegendOpts(is_show=False),
        #     )
        #     .render('resultFile/'+fileName)
        # )
        
        print(fileName+'已生成！')
